# -*- coding: utf-8 -*-
# appengine imports
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.ext.webapp import template

#python imports
import cgi, sys, random, hashlib, urllib, os
from datetime import datetime

#local imports
from baserequesthandler import BaseRequestHandler, BesucherHandler
from models import Frage, Antwort
from einstellungen import APPLICATION_HOST
from utils import get_key_name

#from facebookclient import app_id, FacebookHandler

# suchen
#from whoosh import store
#from whoosh.fields import Schema, STORED, ID, KEYWORD, TEXT
#from whoosh.index import getdatastoreindex
#from whoosh.qparser import QueryParser, MultifieldParser
#import logging

#SEARCHSCHEMA = Schema(frage=TEXT(stored=True), tipp=TEXT(stored=True), akey=ID(stored=True))



#todo: memcache irgendwie einbauen?????

class Handler(BaseRequestHandler):
  def get(self, url_key):
    key = get_key_name(method='sha256', prefix='frage', akey=url_key)
    frage = Frage.get_by_key_name(key)
    if frage is not None:
      antworten = []
      for antwort_key in frage.antworten:
        antwort = Antwort.get_by_key_name(antwort_key)
        if antwort is not None:
          antwort.text = antwort.text.replace('\n','<br/>') # nicht mit antwort.put speichern!
          antworten.append(antwort)

      frage.text = frage.text.replace('\n','<br/>') # nicht mit frage.put speichern!
      template_values = {
       'antworten' : antworten,
       'frage' : frage,
       'leona_server': APPLICATION_HOST
      }
      self.generate('frage/frage.html', template_values)
      return

    else:
      self.error(404)
      self.response.out.write('404 nicht gefunden.')
      return


  def post(self, url_key):
    frage_key = get_key_name(method='sha256', prefix='frage', akey=url_key)
    frage = Frage.get_by_key_name(frage_key)
    text = cgi.escape(self.request.get('text')).encode('utf-8')
    email = cgi.escape(self.request.get('email')).encode('utf-8')

    t = '%s %s' % (text, email)
    if self.is_spam(t):
      self.redirect('/')
      return

    if frage is not None:
      if not frage.beantwortet:
        prefix = '%s %s %s' % (datetime.now(), self.request.remote_addr, text)
        url_key = get_key_name(method='md5', prefix=prefix)
        antwort_key = get_key_name(method='sha256', prefix='antwort', akey=url_key)
        antwort = Antwort.get_by_key_name(antwort_key)
        if antwort is not None:
          self.error(403)
          self.response.out.write('Diese Antwort ist schon vorhanden')
          return
        else:
          frage.antworten.append(antwort_key)
          frage.put()
          antwort = Antwort.get_or_insert(antwort_key)
          antwort.url_key = url_key
          antwort.frage_key = frage_key # todo: rausfinden wie frage.key funktioniert
          antwort.titel = ''
          antwort.text = text.decode('utf-8')
          antwort.gefaellt = 0
          antwort.email = email.decode('utf-8')
          antwort.put()
          self.redirect('/frage/%s' % frage.url_key)
          return
    self.error(404)
    self.response.out.write('Fehler bei der Beantwortung.')
    return


